﻿/*
=============================================================================
Author:魏宝辉通过CodeSmithe生成
CreatedTime:2012-9-9
Description:Send_log三层结构数据访问层
=============================================================================
*/
using System;
using System.Collections.Generic;
using System.Text;
using System.Data;
using System.Data.SQLite;
using FTchina.Models;//引入模型层
namespace FTchina.DAL
{
	public static partial class Send_logService
	{
		#region 增加一条数据
		public static int AddSend_log(Send_log send_log)
		{
			string sql =
			"INSERT INTO Send_log(SendTarget,SendContent,SendTime,result,count) "+
			"VALUES (@SendTarget,@SendContent,@SendTime,@result,@count)";
			
			 
				SQLiteParameter[] para = new SQLiteParameter[]
				{
						
					new SQLiteParameter("@SendTarget",send_log.SendTarget),
						
					new SQLiteParameter("@SendContent",send_log.SendContent),
						
					new SQLiteParameter("@SendTime",send_log.SendTime),
						
					new SQLiteParameter("@result",send_log.Result),
						
					new SQLiteParameter("@count",send_log.Count)
				};
				int newId = DBHelper.ExecuteCommand(sql, para);
				return newId;
			 
		}
		#endregion
		#region 删除数据以主键，标识列，唯一列构建的删除
			
		public static int DeleteSend_logById(long id)	
		{
			string sql="DELETE From  Send_log  Where id=@id";
			 SQLiteParameter[] para = new SQLiteParameter[]
				{
					new SQLiteParameter("@id", id)
				};
				return DBHelper.ExecuteCommand(sql, para);
			 
		}		
		
		#endregion
		#region 修改数据
		//考虑标识列与主键不一致情形
		//以标识列修改实体
		public static int ModifySend_logByid(Send_log send_log)
		{
		string sql ="UPDATE "+"Send_log"+" set "+
					"SendTarget=@SendTarget, "+
					"SendContent=@SendContent, "+
					"SendTime=@SendTime, "+
					"result=@result, "+
					"count=@count "+ 
				" Where id=@id";
			 
				SQLiteParameter[] para = new SQLiteParameter[]
				{
					new SQLiteParameter("@id",send_log.Id),
					new SQLiteParameter("@SendTarget",send_log.SendTarget),
					new SQLiteParameter("@SendContent",send_log.SendContent),
					new SQLiteParameter("@SendTime",send_log.SendTime),
					new SQLiteParameter("@result",send_log.Result),
					new SQLiteParameter("@count",send_log.Count)
				};
				return DBHelper.ExecuteCommand(sql, para);
			 
		}					
		#endregion
		#region 查询数据
		public static IList<Send_log> GetAllSend_logs()
		{
			string sqlAll = "SELECT * FROM Send_log";
			return GetSend_logsBySql(sqlAll);
		}
		//以标识列获得对象
		public static Send_log GetSend_logByid(object id)
		{
			string sql = "SELECT * FROM "+"Send_log"+" WHERE id = @id";
			
            using (SQLiteDataReader reader = DBHelper.GetReader(sql, new SQLiteParameter("@id", id)))
			{
				 
				if (reader.Read())
				{
					Send_log send_log = new Send_log();
					send_log.Id=Convert.ToInt32(reader["id"]);
					send_log.SendTarget=Convert.ToString(reader["SendTarget"]);
					send_log.SendContent=Convert.ToString(reader["SendContent"]);
					send_log.SendTime=Convert.ToDateTime(reader["SendTime"]);
					send_log.Result=Convert.ToString(reader["result"]);
					send_log.Count=Convert.ToString(reader["count"]);
					reader.Close();
					
					return send_log;
					}
				else
				{
					reader.Close();
					return null;
				}
			}
			 
		}
					
		//私有方法供构建好的SQL语句调用
		private static IList<Send_log> GetSend_logsBySql(string safeSql)
		{
			List<Send_log> list = new List<Send_log>();
			 
            using (DataTable table = DBHelper.GetDataTable(safeSql))
			{
				foreach (DataRow row in table.Rows)
				{
					Send_log send_log = new Send_log();
					send_log.Id=Convert.ToInt32(row["id"]);
					send_log.SendTarget=Convert.ToString(row["SendTarget"]);
					send_log.SendContent=Convert.ToString(row["SendContent"]);
					send_log.SendTime=Convert.ToDateTime(row["SendTime"]);
					send_log.Result=Convert.ToString(row["result"]);
					send_log.Count=Convert.ToString(row["count"]);
					list.Add(send_log);
				}
				return list;
			}
             
		}
		private static IList<Send_log> GetSend_logsBySql( string sql, params SQLiteParameter[] values )
		{
			List<Send_log> list = new List<Send_log>();
			 
            using (DataTable table = DBHelper.GetDataTable(sql, values))
			{
				foreach (DataRow row in table.Rows)
				{
					Send_log send_log = new Send_log();
					send_log.Id=Convert.ToInt32(row["id"]);
					send_log.SendTarget=Convert.ToString(row["SendTarget"]);
					send_log.SendContent=Convert.ToString(row["SendContent"]);
					send_log.SendTime=Convert.ToDateTime(row["SendTime"]);
					send_log.Result=Convert.ToString(row["result"]);
					send_log.Count=Convert.ToString(row["count"]);
					list.Add(send_log);
				}
				return list;
			}
            
			 
		}
		#endregion
		 
	}
}

